'use strict';
//
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	//获取数据库的对象
	const db = uniCloud.database();
	//高级查询的操作对象
	const cdm= db.command;
	//获取表的对象
	const tb = db.collection("job");
	//根据类型进行操作
	switch (event.type) {
		//查找所有岗位
		case 'jobs': {
			let res = await tb.get();
			return res;
		};
		//查询，将符合条件的数据筛选出来，再将结果返回  
		case "selectByArr": {
			let{natureOfWorkCheckBoxListArr,educationalRequirementsCheckBoxListArr,salaryCheckBoxListArr}=event
			console.log(natureOfWorkCheckBoxListArr.length)
			if(natureOfWorkCheckBoxListArr.length!=0&&educationalRequirementsCheckBoxListArr.length!=0&&educationalRequirementsCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				
				return {status:200,data:data}
			}else if(natureOfWorkCheckBoxListArr.length==0&&educationalRequirementsCheckBoxListArr.length==0&&salaryCheckBoxListArr.length!=0){
			 let data=await tb.where(
			 {	
				// job_nature:cdm.in(natureOfWorkCheckBoxListArr),
				// educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
				job_salary:cdm.in(salaryCheckBoxListArr)
			 }).get()
			return {status:200,data:data}
			}else if(natureOfWorkCheckBoxListArr.length==0&&salaryCheckBoxListArr.length==0&&educationalRequirementsCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					// job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					// job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				return {status:200,data:data}
			}else if(educationalRequirementsCheckBoxListArr.length==0&&salaryCheckBoxListArr.length==0&&natureOfWorkCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					// educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					// job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				return {status:200,data:data}
			}else if(natureOfWorkCheckBoxListArr.length==0&&educationalRequirementsCheckBoxListArr.length!=0&&salaryCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					// job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				return {status:200,data:data}
			}else if(educationalRequirementsCheckBoxListArr.length==0&&natureOfWorkCheckBoxListArr.length!=0&&salaryCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					// educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				return {status:200,data:data}
			}else if(salaryCheckBoxListArr.length==0&&natureOfWorkCheckBoxListArr.length!=0&&educationalRequirementsCheckBoxListArr.length!=0){
				let data=await tb.where(
				{	
					job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					// job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				return {status:200,data:data}
			}else if(natureOfWorkCheckBoxListArr.length==0&&educationalRequirementsCheckBoxListArr.length==0&&educationalRequirementsCheckBoxListArr.length==0){
				let data=await tb.where(
				{	
					// job_nature:cdm.in(natureOfWorkCheckBoxListArr),
					// educationalRequirements:cdm.in(educationalRequirementsCheckBoxListArr),
					// job_salary:cdm.in(salaryCheckBoxListArr)
				}).get()
				
				return {status:200,data:data}
			}
			
		};
		//根据id查找岗位及它所属于的公司(联表)
		case "showAllJob": {
		//查找
		let data=await tb.aggregate().lookup({
			from:'company',
			localField:'c_id',
			foreignField:'_id',
			as:'jobsList'
		}).end()
		return {status:200,data:data}
		};
		//添加一个岗位
		case "add": {
			let res = await tb.add({
				type
			});
			return res;
		};
		//根据id删除岗位
		case "deleteById": {
			let _id = event.id;
			let res = await tb.doc(_id).remove()
			return res;
		};
		//根据cid查找 某公司下的企业
		case "selectJobsByCid":{
			let{cid}=event;
			let data=await tb.where({
				cid:cid
			}).get();
			return {state:200,data:data}
		};
	}
};
